<template>
	<view class="container">
		<view class="example">
			<avatar ref="avatar" @imgOk="imgOk"></avatar>
			<uni-forms ref="form" :model="user" labelWidth="80px">
				<uni-forms-item label="登录账号">
					<uni-easyinput v-model="user.username" disabled="" placeholder="请输入昵称" />
				</uni-forms-item>
				<!-- <uni-forms-item label="中文名称">
					<uni-easyinput v-model="user.chineseName" placeholder="请输入昵称" />
				</uni-forms-item> -->
				<uni-forms-item label="名称">
					<uni-easyinput type="nickname" primaryColor="#00ac04" v-model="user.realName" @blur="onblur"
						placeholder="请输入昵称" />

				</uni-forms-item>
				<uni-forms-item label="手机号码" name="mobile">


					<uni-easyinput primaryColor="#00ac04" v-model="user.mobile" placeholder="请输入手机号码" />

					<button open-type="getPhoneNumber"
						class="margin-top-xs login-btn cu-btn block bg-green-fan  radius "
						@getphonenumber="onGetPhoneNumber">授权并获取手机号</button>
				</uni-forms-item>


			</uni-forms>
			<button class="margin-top-xl  block bg-olive  radius" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import avatar from "@/pages/mine/avatar/index.vue"
	import storage from '@/utils/storage'
	import {
		getUserProfile,
		getPhoneByWeChatCode
	} from "@/api/system/user"
	import {
		updateUserProfile
	} from "@/api/system/user"
	import {
		doUpdate
	} from '@/api/system/user/userManagement'

	export default {
		components: {
			avatar
		},
		data() {
			return {
				user: {
					avatar: "",
					username: "",

					chineseName: "",
					realName: "",
					mobile: "",
					email: "",
					sex: ""
				},
				sexs: [{
					text: '男',
					value: "0"
				}, {
					text: '女',
					value: "1"
				}],
				rules: {

					realName: {
						rules: [{
							required: true,
							errorMessage: '昵称不能为空'
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: '手机号码不能为空'
						}, {
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							errorMessage: '请输入正确的手机号码'
						}]
					},

				}
			}
		},
		onLoad() {
			this.getUser()
		},
		onReady() {
			this.$refs.form.setRules(this.rules)

		},
		onShow() {
			this.$refs.avatar.open()
		},
		methods: {
			onGetPhoneNumber(e) {
				console.log(e.detail.code) // 动态令牌
				this.getPhoneByWeChatCode(e.detail.code)
			},

			onblur(e) {
				this.user.realName = e.detail.value
			},

			async getPhoneByWeChatCode(code) {
				let {
					data
				} = await getPhoneByWeChatCode({
					code: code
				})
				this.user.mobile = data.phoneNumber

			},

			imgOk(e) {
				// console.log(e);
				// this.user.avatar = e
				// this.$refs.form.validate().then(res => {
				// 	this.update()
				// 	this.$modal.msgSuccess("修改成功")
				// })
			},
			getUser() {
				console.log(this.$store.state.user.user);
				this.user = this.$store.state.user.user
			},
			async update() {

				await doUpdate(this.user)
				this.$modal.msgSuccess("修改成功")
			},
			submit(ref) {
				// this.$refs.avatar.getImageInfo()
				this.update()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>